﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ExcelLibrary.SpreadSheet;
using System.Globalization;

namespace Conversor
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] filenames = Directory.GetFiles("./");
            foreach (var item in filenames)
            {
                if (item.Split(new string[] { "/" }, StringSplitOptions.None )[1].IndexOf('.') < 0)
                    GenerateXls(item);
            }
        }

        public static decimal CustomParse(string incomingValue)
        {
            decimal val;
            if (!decimal.TryParse(incomingValue.Replace(",", "").Replace(".", ""), NumberStyles.Number, CultureInfo.InvariantCulture, out val))
                return 0m;
            return val / 10;
        }

        public static void GenerateXls(string filenameToConvert)
        {
            string filename = filenameToConvert;
            //string newFileName = String.Format("Correntes{0}", filename);
            string newCurrentXlsFile = String.Format("./Tabelas/{0}Dados.xls", filenameToConvert);
            FileStream oldFs = File.Open(filename, FileMode.Open);
            StreamReader oldReader = new StreamReader(oldFs);
            string line = String.Empty;

            //Nova Parte
            Workbook wbCurrent = new Workbook();            
            Worksheet wsCurrent = new Worksheet("Medições de correntes");            

            int num = 0;

            while ((line = oldReader.ReadLine()) != null)
            {
                string[] values = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < values.Length; i++)
                        wsCurrent.Cells[num, i] = new Cell(values[i]);
                ++num;
            }

            wbCurrent.Worksheets.Add(wsCurrent);
            Directory.CreateDirectory("./Tabelas");
            wbCurrent.Save(newCurrentXlsFile);            
        }
    }
}


/*
0 No.s
1 Date
2 Time
3 KW1
4 KVAR1
5 KVA1
6 PF1
7 Theta1
8 V1
9 I1
10 KW2
11 KVAR2
12 KVA2
13 PF2
14 Theta2
15 V2
16 I2
17 KW3
18 KVAR3
19 KVA3
20 PF3
21 Theta3
22 V3
23 I3
24 Hz
25 I4
26 TKW
27 TKVAR
28 TKVA
29 TPF
30 TTheta	
*/